// pages/moreservice/shared_parking/shared_parking.js
import { request } from "../../../request/index.js";
const app = getApp()
Page({
  setTabIndex(e) {
    let activeIndex = e.currentTarget.dataset.index;
    this.setData({
      activeIndex
    })
  },
  /**
   * 页面的初始数据
   */
  data: {

    activeIndex: 0,
    saleportlist: [
    ],
    searchportlist: [
    ],

    parkspaceId:'',//留言测试
    commentinfoList: [],//留言测试
    
    keyValue: '',//搜索框测试
    queryText: null,
  },
  QueryParams: {
    pagenum: 1,
    pagesize: 1000
  },
  totalpages: 1,



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.getsalelist();
    this.getsearchlist();
  },
  async getsalelist() {
    const res = await request({
      url: "http://localhost:8080/ipark/parkshare/salelist",
      data: this.QueryParams,
    });
    console.log(res.data);
    const total = res.data.data.pageshare.total;
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize);
    this.setData({
      saleportlist: [...this.data.saleportlist, ...res.data.data.pageshare.pageshareList],
      // parkspaceId: res.data.data.pageshare.pageshareList.id
    })
    console.log("========以下是saleportlist==========");
    console.log(this.data.saleportlist);
    console.log("========以下是parkspaceId==========");
    console.log(this.data.parkspaceId);
    wx.stopPullDownRefresh();
  },
  async getsearchlist() {
    const res = await request({
      url: "http://localhost:8080/ipark/parksharebuyer/buyerlist",
      data: this.QueryParams,
    });
    console.log(res.data);
    const total = res.data.data.pagebuyer.total;
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize);
    this.setData({
      searchportlist: [...this.data.searchportlist, ...res.data.data.pagebuyer.pagebuyerList]
    }),
    // console.log("=====以下是searchportlist=========");
    // console.log(this.data.searchportlist);
    wx.stopPullDownRefresh();
  },
  toadd: function () {
    wx.navigateTo({
      url: '/pages/moreservice/shared_parking/addshare/addshare',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      saleportlist: [],
      searchportlist: []
    })
    this.QueryParams.pagenum = 1;

    this.getsalelist();

    this.getsearchlist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    if (this.QueryParams.pagenum >= this.totalPages) {
      wx.showToast({
        title: '已经到底了',
      });

    } else {
      this.QueryParams.pagenum++;
      if (this.data.activeIndex === 0) {
        this.getsalelist();
      } else {
        this.getsearchlist();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //留言测试
  // 留言测试API
  getPageList(parkspaceId) {
    var that = this;
    wx.request({
      // url: 'http://localhost:8080/ipark/parksharecomment/commentinfo?parkspaceId=' + parkspaceId,
      url: 'http://localhost:8080/ipark/parksharecomment/commentinfo/40',
      success: (result) => {
        that.setData({
          commentinfoList: result.data.data.list
        })
        console.log(result);
        console.log(that.data.commentinfoList);
      }
    })
  },

  toparksharecomment: function () {
    // var that = this;
    wx.redirectTo({
      url: '/pages/moreservice/shared_parking/parksharecomment/parksharecomment',
    })
    // parkspaceId: res.data.data.pageshare.pageshareList.id
  },

  // addComment(comment) {
  //   wx.request({
  //     url: 'http://localhost:8080/ipark/parksharecomment/auth/addComment',
  //     method:'post',
  //     success: (result) => {
  //       wx.showToast({
  //         title: '留言成功！',
  //         duration: 2000,
  //       })
  //     }
  //   })
  // },

  // 留言测试API

  //留言测试js

  //获取车位详细信息
  // getCourseInfo() {
  //   courseApi.getFrontCourseInfo(this.course.courseId).then((resp) => {
  //     this.chapterList = resp.data.data.chapterVideoList;
  //     this.course = resp.data.data.courseWebVo;
  //     this.course.courseId = resp.data.data.courseWebVo.id;
  //   });
  // },

  // initComment() {
  //   comment
  //     .getPageList(this.page, this.limit, this.saleportlist.parkspaceId)
  //     .then((response) => {
  //       this.data = response.data.data;
  //       console.log(response.data.data);
  //     });
  // },
  
  // addComment() {
  //   this.comment.parkspaceId = this.course.parkspaceId;
  //   this.comment.teacherId = this.course.teacherId;
  //   comment.addComment(this.comment).then((response) => {
  //     if (response.data.success) {
  //       this.$message({
  //         message: "评论成功",
  //         type: "success",
  //       });
  //       this.comment.content = "";
  //       this.initComment();
  //     }
  //   });
  // },

  // gotoPage(page) {
  //   comment.getPageList(page, this.limit, this.courseId).then((response) => {
  //     this.data = response.data.data;
  //   });
  // },
  //留言测试js
  //留言测试


  //搜索框测试方案二
  getInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      queryText: e.detail.value
    })
    this.carlist(e.detail.value)
  },
  carlist(keyValue) {
    var that = this;
    console.log('====', this.queryText)
    wx.request({
      url: 'http://localhost:8080/ipark/parkshare/salelist/name?parkAddress=' + keyValue,
      success: (result) => {
        that.setData({
          saleportlist: result.data.data.list
        })
        console.log(result);
        // console.log(queryText);
      }
    });
  },
  //搜索框测试方案二
})